﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlogManager.Core.BaseArchitecture;

namespace BlogManager.Application
{
    public partial class Page : AppFormBase
    {
        private int? pageId = null;
        private int? siteId = null;

        public override void PageLoad()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["pageId"]))
                pageId = Convert.ToInt32(Request.QueryString["pageId"]);

            if (!string.IsNullOrEmpty(Request.QueryString["siteId"]))
                siteId = Convert.ToInt32(Request.QueryString["siteId"]);
        }

        public override void PageFirstTimeLoad()
        {
            //TextBoxForDate.Text = DateTime.Now.ToShortDateString();
        }

        public override void PageDataLoad()
        {
            if (pageId != null)
            {
                var pageData = Data.Entities.Page.SingleOrDefault(x => x.Id == pageId.Value);

                TextBoxForTitle.Text = pageData.Title;
                TextBoxForSEOUrl.Text = pageData.SEOUrl;
                CKEditorForContent.Text = pageData.Content;

                CheckBoxForShouldAppearInMenu.Checked = pageData.ShouldAppearInMenu;
                TextBoxForMenuText.Text = pageData.MenuText;
                TextBoxForOrderInMenu.Text = pageData.OrderInMenu.ToString();
                DropDownListForParentPage.SelectedValue = pageData.ParentPageId.ToString();
            }
        }

        protected void ButtonForSaving_Click(object sender, EventArgs e)
        {
            Save();
        }
        protected void ButtonForSavingAndReturning_Click(object sender, EventArgs e)
        {
            Save();
            MessagesForActions.ReturnToCallingPage();
        }

        public override void Save()
        {
            //If we have a pageId then we load it.
            var pageData = (pageId == null) ? new Data.Entities.Page() : Data.Entities.Page.SingleOrDefault(x => x.Id == pageId);

            pageData.SiteId = siteId.Value;
            pageData.Title = TextBoxForTitle.Text;
            pageData.SEOUrl = TextBoxForSEOUrl.Text;

            pageData.Content = CKEditorForContent.Text;
            

            pageData.ShouldAppearInMenu= CheckBoxForShouldAppearInMenu.Checked;

            pageData.MenuText=TextBoxForMenuText.Text;
            if(!string.IsNullOrEmpty(TextBoxForOrderInMenu.Text))
                pageData.OrderInMenu = Convert.ToInt32(TextBoxForOrderInMenu.Text);
            if (!string.IsNullOrEmpty(DropDownListForParentPage.SelectedValue))
            pageData.ParentPageId = Convert.ToInt32(DropDownListForParentPage.SelectedValue);

            try
            {
                pageData.Save();

                MessagesForActions.Show = true;
                MessagesForActions.Message = "Saved!";
            }
            catch (Exception ex)
            {
                MessagesForActions.Show = true;
                MessagesForActions.Message = "Error!";
            }
        }

        public override void Delete()
        {
            Data.Entities.Page.Delete(x => x.Id == pageId);
        }
    }
}